package com.find.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.find.answer.domain.dto.PageDto;
import com.find.answer.domain.dto.SetmealDto;
import com.find.answer.domain.po.Category;
import com.find.answer.domain.po.Setmeal;
import com.find.answer.domain.po.SetmealDish;
import com.find.answer.mapper.SetmealMapper;
import com.find.answer.service.CategoryService;
import com.find.answer.service.SetmealDishService;
import com.find.answer.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Seon
 * @date 2022/10/25 15:23
 */
@Service
@Transactional
public class SetmealServiceImpl
        extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    @Override
    public Page<SetmealDto> getSetmealDtoPage(PageDto pageDto, String name) {
        Page<Setmeal> setmealPage = new Page<>();
        Page<SetmealDto> setmealDtoPage = new Page<>();
        setmealPage.setCurrent(pageDto.getPage());
        setmealPage.setSize(pageDto.getPageSize());

        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.like(name != null, Setmeal::getName, name);
        setmealWrapper.orderByDesc(Setmeal::getUpdateTime);
        this.page(setmealPage,setmealWrapper);

        BeanUtils.copyProperties(setmealPage, setmealDtoPage, "records");
        List<Setmeal> setmealList = setmealPage.getRecords();
        LambdaQueryWrapper<SetmealDish> dishWrapper = new LambdaQueryWrapper<>();

        List<SetmealDto> collect = setmealList.stream().map(items -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(items, setmealDto);
            Long setmealId = items.getId();
            Long categoryId = items.getCategoryId();
            Category category = categoryService.getById(categoryId);

            dishWrapper.eq(SetmealDish::getSetmealId, setmealId);
            List<SetmealDish> list = setmealDishService.list(dishWrapper);
            setmealDto.setSetmealDishes(list);
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(collect);
        return setmealDtoPage;
    }

    @Override
    public SetmealDto getSetmealWithDish(Long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = this.getById(id);
        BeanUtils.copyProperties(setmeal, setmealDto);

        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);

        List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    @Override
    public void addSetmealWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        List<SetmealDish> collect = setmealDishes.stream().map(items -> {
            items.setSetmealId(setmealDto.getId());
            return items;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(collect);
    }

    @Override
    public void deleteSetmealWithDish(List<Long> ids) {
        this.removeByIds(ids);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        ids.forEach(id -> {
            wrapper.eq(SetmealDish::getSetmealId, id);
            setmealDishService.remove(wrapper);
        });
    }

    @Override
    public void updateSetmealWithDish(SetmealDto setmealDto) {
        this.updateById(setmealDto);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(wrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(items -> {
            items.setSetmealId(setmealDto.getId());
            return items;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }
}
